package STUDENT.HOANGQUOCVIET.OOPs.SWING;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Demo1 extends JFrame {

	private JPanel contentPane;
	private JTextField txtNumA;
	private JTextField txtNumB;
	private JTextField txtResult;
	private final Action action = new SwingAction();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Demo1 frame = new Demo1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Demo1() {
		setFont(new Font("Times New Roman", Font.PLAIN, 18));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtNumA = new JTextField();
		txtNumA.setBounds(230, 46, 86, 20);
		contentPane.add(txtNumA);
		txtNumA.setColumns(10);
		
		txtNumB = new JTextField();
		txtNumB.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				
			}
		});
		txtNumB.setBounds(230, 94, 86, 20);
		contentPane.add(txtNumB);
		txtNumB.setColumns(10);
		
		txtResult = new JTextField();
		txtResult.setColumns(10);
		txtResult.setBounds(230, 139, 86, 20);
		contentPane.add(txtResult);
		
		JLabel lblNewLabel = new JLabel("Số A");
		lblNewLabel.setBounds(71, 49, 46, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblSB = new JLabel("Sô B");
		lblSB.setBounds(71, 97, 46, 14);
		contentPane.add(lblSB);
		
		JLabel lblKtQu = new JLabel("Kết Quả");
		lblKtQu.setBounds(71, 142, 46, 14);
		contentPane.add(lblKtQu);
		
		JButton btnKtQuCa = new JButton("Kết quả của phép A / B");
		btnKtQuCa.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				double kq;
				if(Double.parseDouble(txtNumB.getText().trim()) == 0){
					JOptionPane.showMessageDialog(null,"Không thể chia cho số 0");					
					txtResult.setText("");
					return;
				}
				try{
					Double.parseDouble(txtNumB.getName().trim());
				}
				catch(NumberFormatException ex){
					JOptionPane.showMessageDialog(null, "Wrong Format:"+ex.toString());
					txtNumB.requestFocus();
					txtResult.setText("");
				}
				kq = Double.parseDouble(txtNumA.getText().trim())/Double.parseDouble(txtNumB.getText().trim());
				txtResult.setText(""+kq);
			}
		});
		btnKtQuCa.setAction(action);
		btnKtQuCa.setBounds(153, 198, 163, 23);
		contentPane.add(btnKtQuCa);
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
}
